package threadpool;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class T03_CallableTest {
    public static void main(String[] args) throws Exception {
        //Callable 类似Runnable  但是有返回值 并且可以抛出异常
        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(3000);
                return "call me!";
            }
        };
        String call = callable.call();//直接执行任务  同步


        // callable一般配合线程池进行使用 ，不向上面一样这么直接调起

        ExecutorService executorService = Executors.newCachedThreadPool();
        //使用ExecutorService异步提交一个任务
        Future<String> submit = executorService.submit(callable);
        System.out.println("before");
        //get() 会阻塞
        String s = submit.get();
        System.out.println("after");

        executorService.shutdown();


    }
}
